package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.ArticleComment;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.mapper.ArticleCommentMapper;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.service.IArticleCommentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 游记的评论
 */
@RestController
@RequestMapping("/articleComment")
public class ArticleCommentController {
    @Autowired
    private IArticleCommentService commentService;
    @Autowired
    private ArticleCommentMapper commentMapper;

    @GetMapping("/articles/{travelId}")
    public PageInfo listCommends(TravelCommendQueryObject queryObject) {
        /* 查询的结果需要按推荐的时间降序排列*/
        queryObject.setOrderBy("art.createTime desc");
        PageInfo<ArticleComment> pageInfo = commentService.queryForComments(queryObject);
        for (ArticleComment ele : pageInfo.getList()) {
            TravelCommendQueryObject childer = new TravelCommendQueryObject();
            childer.setParentId(ele.getId());  // 设置指定查找父亲评论的id
            List<ArticleComment> childerQuery = commentMapper.queryCommentsByArticleId(childer);
            ele.setCommentList(childerQuery);
            ele.setCommentNums(childerQuery.size());
           /* int likeNum = likeMapper.selectByCommentIdLike(ele.getId());
            ele.setLikeNum(likeNum);*/
        }
        return pageInfo;
    }

    /**
     * 保存游记的评论
     * @param articleComment
     * @return
     */
    @PostMapping("/{travel.id}/comment")
    public JsonResult saveComment(ArticleComment articleComment) {
        commentService.saveArticleComment(articleComment);
        return new JsonResult();
    }
    /**
     * 保存评论的评论
     * @param articleComment
     * @return
     */
    @PostMapping("/{parent.id}/replyComment")
    public JsonResult saveReplyComment(ArticleComment articleComment) {
        commentService.saveArticleComment(articleComment);
        return new JsonResult();
    }

    /**
     * 获取单条子评论
     *
     * @param queryObject
     * @return
     */
    @GetMapping("/comment/{commentId}")
    public PageInfo<ArticleComment> oneCommendById(TravelCommendQueryObject queryObject) {
        /* 查询的结果需要按推荐的时间降序排列*/
        queryObject.setOrderBy("art.createTime desc");
        PageInfo<ArticleComment> articleComment = commentService.selectForCommendById(queryObject);
        for (ArticleComment ele : articleComment.getList()) {
            TravelCommendQueryObject childer = new TravelCommendQueryObject();
            childer.setParentId(ele.getId());  // 设置查找指定父亲评论的id
            List<ArticleComment> childerQuery = commentMapper.queryCommentsByArticleId(childer);
            ele.setCommentList(childerQuery);
            ele.setCommentNums(childerQuery.size());
           /*int likeNum = likeMapper.selectByCommentIdLike(ele.getId());
            ele.setLikeNum(likeNum);*/
        }
        return articleComment;
    }

    /**
     * 获取当前评论下的子评论集合
     *
     * @param queryObject
     * @return
     */
    @GetMapping("/{parentId}/comments")
    public PageInfo queryCommendById(TravelCommendQueryObject queryObject) {
        /* 查询的结果需要按推荐的时间降序排列*/
        queryObject.setOrderBy("art.createTime desc");
        PageInfo<ArticleComment> pageInfo = commentService.queryForComments(queryObject);

        return pageInfo;
    }
    // 评论子集
    @GetMapping("/replyComment/{parentId}")
    public PageInfo<ArticleComment> listCommendById(TravelCommendQueryObject queryObject) {
        /* 查询的结果需要按推荐的时间降序排列*/
        queryObject.setOrderBy("art.createTime desc");
        PageInfo<ArticleComment> articleComment = commentService.selectForCommendById(queryObject);
        for (ArticleComment ele : articleComment.getList()) {
            TravelCommendQueryObject childer = new TravelCommendQueryObject();
            childer.setParentId(ele.getId());  // 设置查找指定父亲评论的id
            List<ArticleComment> childerQuery = commentMapper.queryCommentsByArticleId(childer);
            ele.setCommentList(childerQuery);
            ele.setCommentNums(childerQuery.size());
           /*int likeNum = likeMapper.selectByCommentIdLike(ele.getId());
            ele.setLikeNum(likeNum);*/
        }
        return articleComment;
    }

}
